package com.pe.pgn.clubpgn.webapp.controller.promocionbono;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;

import org.apache.abdera.protocol.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;

import com.pe.pgn.clubpgn.common.CLPConstantes;
import com.pe.pgn.clubpgn.domain.ClpbPromocionBono;
import com.pe.pgn.clubpgn.domain.ClpdRangoPromocion;
import com.pe.pgn.clubpgn.domain.ClpdTipoBono;
import com.pe.pgn.clubpgn.domain.beans.BNPromocionBono;
import com.pe.pgn.clubpgn.domain.beans.BNPromocionEstacion;
import com.pe.pgn.clubpgn.service.PromocionBonoService;
import com.pe.pgn.clubpgn.service.TipoBonoService;
import com.pe.pgn.clubpgn.webapp.controller.BaseFormController;

@Controller
@RequestMapping("/promocionbono/**")
public class PromocionBonoController extends BaseFormController{

	@Autowired TipoBonoService tipoBonoService;
	
	@Autowired PromocionBonoService promocionBonoService;
	
	@RequestMapping(value = "/promocionbonolist.html")
    public void listarPromocionesBono(Model model) {

		List<ClpbPromocionBono> listPromocionBono = promocionBonoService.listarPromocionesBono(false);
		model.addAttribute("listPromocionBono", listPromocionBono);
    }
	
	@RequestMapping(value = "/promocionbonoform.html", method = RequestMethod.GET)
	public @ModelAttribute("promocionbonoform") ClpbPromocionBono verPromocionBono(
		   @RequestParam(value = "id", required = false) Long id, Model model, HttpServletRequest request) {

		List<ClpdTipoBono> listTipoBono = tipoBonoService.listarTiposBono(true);
		ClpbPromocionBono bean  = new ClpbPromocionBono();
		
		if (id != null) {
			
			bean = promocionBonoService.obtenerPromocionBono(id);
			boolean existeRangos = promocionBonoService.verificarSiTieneRangos(id);
			
			if(!existeRangos){
				model.addAttribute("msgRangos" , getText("rangoPromocionBonoForm.sinrangos", request.getLocale()));	
			}else{
				
				Map<String,Object> map = promocionBonoService.validarRegistroRangoFinal(id.toString());
				if(map.get(CLPConstantes.MSG).equals(CLPConstantes.ERROR)){
					if(map.get("detalle").equals("no_existe_rango_final")){
						model.addAttribute("msgRangos" , getText("rangoPromocionBonoForm.sinrangofinal", request.getLocale()));
					}
				}
			}
		}else{			
			bean.setStPromocionBono(true);
		}
		
		model.addAttribute("promocionbonoform" , bean);
		model.addAttribute("listTipoBono" , listTipoBono);
		return bean;
	}
	
	@RequestMapping(value = "/promocionbonoform.html", method = RequestMethod.POST)
	public String guardarPromocionBono(
			@ModelAttribute("promocionbonoform") ClpbPromocionBono promocionBono,
			BindingResult result, SessionStatus status, HttpServletRequest request, Model model) {

		try {

			List<ClpdTipoBono> listTipoBono = tipoBonoService.listarTiposBono(true);
			model.addAttribute("listTipoBono" , listTipoBono);
			
			if (result.hasErrors()) {
				model.addAttribute("promocionbonoform", promocionBono);
				return "/promocionbono/promocionbonoform";
			}		
			
			Map<String, Object> mapMsg = promocionBonoService.guardarPromocionBono(promocionBono);
			if(mapMsg.get(CLPConstantes.MSG).equals(CLPConstantes.SUCCESS)){
				saveMessage(request, getText("common.added", request.getLocale()));
				return "redirect:promocionbonolist.html";
			}else{
				
				if(mapMsg.get("detalle").equals("promocion_duplicidad_desc")){
					saveError(request, getText("promocionbonoform.duplicidad.desc", request.getLocale()));
					return "/promocionbono/promocionbonoform";
				}else if(mapMsg.get("detalle").equals("promocion_validatemontomin")){
					saveError(request, getText("promocionbonoform.validatemontomin", request.getLocale()));
					return "/promocionbono/promocionbonoform";
				}else if(mapMsg.get("detalle").equals("promocion_traslape")){
					saveError(request, getText("promocionbonoform.traslape", request.getLocale()));
					return "/promocionbono/promocionbonoform";
				}else if(mapMsg.get("detalle").equals("promocion_validacion.deshabilito")){
					saveError(request, getText("promocionbonoform.validacion.deshabilito", request.getLocale()));
					return "/promocionbono/promocionbonoform";
				}else{
					saveError(request, getText("errors.general", request.getLocale()));
					return "/promocionbono/promocionbonoform";
				}				
			}
			
		} catch (Exception e) {
			saveError(request, getText("errors.general", request.getLocale()));
			return "/promocionbono/promocionbonoform";
		}
	}
	
	@RequestMapping(value = "/eliminarpromocionbono.html")
	public String eliminarpromocionbono(@RequestParam(value = "id", required = false) Long id, 
			HttpServletRequest request, Model model) {

		try {

			Map<String,Object> map = promocionBonoService.eliminarPromocionBono(id);
			int rowsDeleted = map.get("rowsDeleted") != null ? Integer.parseInt(map.get("rowsDeleted").toString()) : 0;
			
			if(rowsDeleted > 0){
				saveMessage(request, getText("common.deleted", request.getLocale()));
				return "redirect:promocionbonolist.html";
			}else{
				
				if(map.get("detalle").equals("promocion_validacion_eliminar")){
					saveError(request, getText("promocionbonoform.validacion.eliminar", request.getLocale()));
					return "redirect:promocionbonoform.html?id="+id;
				}else{
					saveError(request, getText("errors.general", request.getLocale()));
					return "redirect:promocionbonoform.html?id="+id;
				}
			}		
			
		} catch (Exception e) {
			saveError(request, getText("errors.general", request.getLocale()));
			return "redirect:promocionbonoform.html?id="+id;
		}
	}
	
	@RequestMapping(value = "/rangosPromocionBono.html")
	public String verRangosPromocion(@RequestParam(value = "id", required = true) Long id, 
			ModelMap modelMap) {

		List<ClpdRangoPromocion> listadoRangos = promocionBonoService.listarRangoPromocion(id);
		ClpbPromocionBono promocionBono = promocionBonoService.obtenerPromocionBono(id);
		
		modelMap.addAttribute("rangoPromocionBono", new ClpdRangoPromocion());
		modelMap.addAttribute("promocionBono", promocionBono);
		modelMap.addAttribute("listadoRangos", listadoRangos);
		
		return "/promocionbono/promocionBonoRangos";
	}
	
	
	@RequestMapping(value = "/promocionEstacion.html", method=RequestMethod.GET)
	public @ModelAttribute("promocionBono")BNPromocionBono 
	verPromocionEstacion(@RequestParam(value = "id", required = true) Long id, 
			Model model, HttpServletRequest request) {

		List<BNPromocionEstacion> listadoPromocionxEstacion = promocionBonoService.listadoPromocionBono(id);
		BNPromocionBono promocionBono = promocionBonoService.obtenerBNPromocionBono(id);
		promocionBono.setListaEstaciones(listadoPromocionxEstacion);
		promocionBono.setContador(promocionBono.getListaEstaciones().size());
		
		return promocionBono;
	} 
	
	
	@RequestMapping(value = "/promocionEstacion.html", method=RequestMethod.POST)
	public String guardarPromocionxEstacion( 
	@ModelAttribute("promocionBono")BNPromocionBono promocionBo,
	BindingResult result, SessionStatus status, HttpServletRequest request, Model model) {

		
		Map<String, Object> map = new HashMap<String, Object>();
		
		map=promocionBonoService.guardarPromocionBonoEstacion(promocionBo);
		
		if(map.get(CLPConstantes.MSG).equals(CLPConstantes.SUCCESS)){
			saveMessage(request, getText("common.added", request.getLocale()));
		}else if(map.get(CLPConstantes.MSG).equals(CLPConstantes.ERROR)){
			saveError(request, getText("promocionbonoform.error", request.getLocale()));
		}else if(map.get(CLPConstantes.MSG).equals(CLPConstantes.ESPACIO_VACIO)){
			saveMessage(request, getText("common.added", request.getLocale()));
		}

		return "redirect:promocionEstacion.html?id=" + promocionBo.getCoPromocionBono();
		
	}
	
	

	
}
